package com.gouaixin.bee.beeframework2.model;

import android.content.Context;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.gouaixin.bee.beeframework2.activity.MyMainActivity;
import com.gouaixin.bee.beeframework2.bean.getChatFriendsResponse;
import com.gouaixin.bee.beeframework2.tool.XHttpUtils;
import com.gouaixin.bee.beeframework2.tool.mLog;

import java.util.ArrayList;

/**
 * 作者：Fisher on 2015/5/6 10:27
 * 邮箱：630709658@qq.com
 * 座右铭:知识来自积累,经验源于总结
 */
//刷新底部的信箱数字
public class NumberModel {
    public static    ArrayList<getChatFriendsResponse> datas = new ArrayList<getChatFriendsResponse>();

    /***
     * 设置底部信箱的数字
     * 1.遍历集合，获取每个元素
     * 2.得到每个元素的信箱数字
     * 3.求和
     */
    public static    void refreshMailNumber(final Context context) {

        mLog.e("刷新底部信箱的数字");

        String url = "http://jiekou.hydgyx.com/api/interface_chatfriends.php";
        final String cid= PreferencesUtils.getString(context, "cid");
        RequestParams params = new RequestParams();
        params.addBodyParameter("cid", cid);
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST,
                url,
                params,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                        mLog.e(current + "/" + total);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        mLog.e("获取聊天好友列表：响应=" + XHttpUtils.Unicode2Chinese(responseInfo.result));
                        String json = XHttpUtils.Unicode2Chinese(responseInfo.result);
                        if (!json.equals("")) {
                            datas = new Gson().fromJson(json, new TypeToken<ArrayList<getChatFriendsResponse>>() {
                            }.getType());

                            if (!datas.isEmpty()) {
                                int num=0;
                                if(datas!=null){
                                    for(int i=0;i<datas.size();i++){
                                        getChatFriendsResponse bean=datas.get(i);
                                        String jid=bean.getId();
                                        int temp= PreferencesUtils.getInt(context, jid + "MailNumber", 0);
                                        num+=temp;
                                    }
                                }

                                int fangkeNumber= PreferencesUtils.getInt(context, cid + "Fangke", 0);
                                int guanzhuNumber= PreferencesUtils.getInt(context, cid + "Guanzhu", 0);
                                int ZhenxinhuaFriendNumber= PreferencesUtils.getInt(context, cid + "ZhenxinhuaFriend", 0);
                                int MishuMsgNumber= PreferencesUtils.getInt(context, "MishuMsg", 0);
                                num=num+fangkeNumber+guanzhuNumber+ZhenxinhuaFriendNumber+MishuMsgNumber;

                                if(num==0){
                                    MyMainActivity.mail_num_bg.setVisibility(View.GONE);
                                }else {
                                    MyMainActivity.mail_num_bg.setVisibility(View.VISIBLE);
                                    MyMainActivity.mail_num.setText(num+"");
                                }
                            }
                        }
                    }

                    @Override
                    public void onStart() {
                        mLog.e("开始请求：");
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        mLog.e("请求故障：" + msg);
                    }
                }
        );


    }


    /**
     * 获取聊天好友列表-响应值处理
     *
     * @param json
     */
    private void onGetChatFriends(String json) {
//        mLog.e("m2的json:" + json);


    }


}
